//
//  ZJSettingController.m
//  Lottery
//
//  Created by ZJ on 2018/8/15.
//  Copyright © 2018年 iCatch Technology Inc. All rights reserved.
//

#import "ZJSettingController.h"
#import "ZJRedeemController.h"
#import "ZJSettingCell.h"

#import <MessageUI/MessageUI.h>

@interface ZJSettingController () <MFMessageComposeViewControllerDelegate>

@property (nonatomic, strong) NSArray *groups;

@end

@implementation ZJSettingController

- (instancetype)init {
    return [super initWithStyle:UITableViewStyleGrouped];
}

- (instancetype)initWithStyle:(UITableViewStyle)style {
    return [super initWithStyle:UITableViewStyleGrouped];
}

- (NSArray *)groups {
    if (_groups == nil) {
        // 获取路径
        NSString *path = [[NSBundle mainBundle] pathForResource:self.plistName ofType:@"plist"];
        
        // 解析成数组
        _groups = [NSArray arrayWithContentsOfFile:path];
    }
    
    return _groups;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    [self setupGUI];
}

- (void)setupGUI {
    // 设置标题
    self.navigationItem.title = @"设置";
    
    // 创建返回按钮
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"NavBack"] style:UIBarButtonItemStylePlain target:self action:@selector(backClick)];
    
    // 设置 leftbaritem
    self.navigationItem.leftBarButtonItem = item;
}

- (void)backClick {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

// 点击 cell 调用
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    // 获取组
    NSDictionary *group = self.groups[indexPath.section];
    // 获取所有的 cell
    NSArray *items = group[@"items"];
    // 获取当前的 cell 信息
    NSDictionary *item = items[indexPath.row];
    
    NSString *targetVC = item[@"targetVC"];
    if (targetVC && targetVC.length > 0) {
        Class cls = NSClassFromString(targetVC);
        UIViewController *vc = [[cls alloc] init];
        vc.navigationItem.title = item[@"title"]; // 设置 title
        
        if ([vc isKindOfClass:[ZJSettingController class]]) {
            // 如果 setting 类型的 需要传入一个 plistname
            ZJSettingController *setting = (ZJSettingController *)vc;
            setting.plistName = item[@"plistName"];
        }
        
        // 跳转
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    NSString *funcName = item[@"funcName"];
    // 执行 item[@"funcName"] 方法
    
    if (funcName && funcName.length > 0) {
        // 把字符串转化成一个方法
        SEL func = NSSelectorFromString(funcName);
        
        // 执行方法
        [self performSelector:func withObject:item[@"subTitle"] afterDelay:0];
    }
    
//    if (indexPath.section == 0 && indexPath.row == 0) {
//        // 使用兑换码
//        ZJRedeemController *vc = [[ZJRedeemController alloc] init];
//        [self.navigationController pushViewController:vc animated:YES];
//    }
}

#pragma mark - Table view data source
// 有多少组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.groups.count;
}

// 某一组有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // 获取组
    NSDictionary *group = self.groups[section];
    // 获取当前组 所有的 cell 信息
    NSArray *items = group[@"items"];
    
    return items.count;
}

// cell长什么样
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    // 获取当前 cell 的信息 ----
    // 1.获取组
    NSDictionary *group = self.groups[indexPath.section];
    // 2.获取当前组的所有的 cell 信息
    NSArray *items = group[@"items"];
    // 3.当前 cell 的信息
    NSDictionary *item = items[indexPath.row];
    
    // 创建cell
    ZJSettingCell *cell = [ZJSettingCell settingCellWithTableView:tableView item:item];
    
    // Configure the cell...
    // 把数据传给 cell
//    cell.item = item;
    
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}

// header 信息
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    // 获取需要展示 header 的组
    NSDictionary *group = self.groups[section];
    
    return group[@"header"];
}

// footer 信息
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
    // 获取需要展示 footer 的组
    NSDictionary *group = self.groups[section];
    
    return group[@"footer"];
}

// 检查新版本
- (void)checkUpdate {
    NSLog(@"你已经是最新的版本了");
}

// 打电话
- (void)makeCall:(NSString *)num {
    num = (num && num.length > 0) ? num : @"10010";
    
    // 获取 app
    UIApplication *app = [UIApplication sharedApplication];
    // 协议头是 tel
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"tel:///%@", num]];
    // 通过 open url 打电话
    [app openURL:url];
}

// 发短信
- (void)makeSms:(NSString *)num {
    num = (num && num.length > 0) ? num : @"10010";

    // 获取 app
//    UIApplication *app = [UIApplication sharedApplication];
//    // 协议头是  sms
//    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"sms:///%@", num]];
//    // 通过 open url 发短信
//    [app openURL:url];
    
    MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
    
    picker.messageComposeDelegate = self;
    
    picker.navigationBar.tintColor = [UIColor blackColor];
    
    picker.body = @"hello";
    
    picker.recipients = @[num];
    
    [self presentViewController:picker animated:YES completion:nil];
}

// 短信发送完成的时候调用
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
    NSLog(@"%zd", result);
}

// 评分支持
- (void)mark {
    // 获取 app
    UIApplication *app = [UIApplication sharedApplication];
    // 协议头是
    NSURL *url = [NSURL URLWithString:@"https://itunes.apple.com/cn/app/id414478124?mt=8&ls=1"];
    // 通过 open url 跳转到应用商店
    [app openURL:url];
}

@end
